﻿package com.tudou.cookie
{
    import flash.net.*;

    public class Cookie extends Object implements ICookie
    {
        protected var locking:Boolean = false;
        protected var so:SharedObject;

        public function Cookie(param1:String)
        {
            so = SharedObject.getLocal(param1, "/");
            return;
        }// end function

        public function getValue(param1:String)
        {
            var name:* = param1;
            try
            {
                return so.data[name];
            }
            catch (error:Error)
            {
                trace(error);
            }
            return null;
        }// end function

        public function unlock() : void
        {
            locking = false;
            flush();
            return;
        }// end function

        public function lock() : void
        {
            locking = true;
            return;
        }// end function

        public function clear() : void
        {
            so.clear();
            return;
        }// end function

        public function deleteValue(param1:String) : void
        {
            var name:* = param1;
            try
            {
                so.data[name] = null;
                delete so.data[name];
                flush();
            }
            catch (error:Error)
            {
                trace(error);
            }
            return;
        }// end function

        public function setValue(param1:String, param2) : void
        {
            so.data[param1] = param2;
            flush();
            return;
        }// end function

        private function flush() : void
        {
            try
            {
                if (!locking)
                {
                    so.flush();
                }
            }
            catch (error:Error)
            {
                trace(error);
            }
            return;
        }// end function

    }
}
